

<?php
//基于SPEEDPHP的无限栏目分级
//作者:jeffrey_shou
//版权归:www.ezsoso.com.cn
class lib_part extends spModel {
	private $m_arrPartTree;
	private $m_arrPartTreeMap;
	private $m_arrCurrentPartNode;
	private $m_arrPartTreeLabel=array();
	private $m_sSignSpace;
	
	var $m_oXMLDom=object;
	
	
	var $pk = "partid"; // 每个留言唯一的标志，可以称为主键
	var $table = "part"; // 数据表的名称
	
	var $addrules=array('isnumber'=>array('lib_part','check_number'));
	var $verifier = array(
		"rules" => array(
			'partid' => array(
				'notnull' => TRUE,
				'minlength' => 1,
				'maxlength' => 20
			),
			'partname'=>array(
				'notnull' => TRUE,
				'minlength' => 1,
				'maxlength' => 50
			),
			'fpartid'=>array(
				'notnull' => TRUE,
				'minlength' => 1,
				'maxlength' => 20 
			),
			'partimage'=>array(
				'maxlength' => 200 
			),
			'ordernum'=>array('isnumber'=>TRUE
			),
			'partdescription'=>array(
				'maxlength' => 500 // acl角色名长度不能大于500		
			),
		),
		"messages" => array( // 提示信息
			'partid' => array(
				'notnull' => "栏目ID不能为空",
				'minlength' => "栏目ID不能少于1个字符",
				'maxlength' => "栏目ID不能大于20个字符"
			),
			'partname' => array(
				'notnull' => "栏目名不能为空",
				'minlength' => "栏目名不能少于1个字符",
				'maxlength' => "栏目名不能大于50个字符"
			),
			'fpartid' => array(
				'notnull' => "父级栏目ID不能为空",
				'minlength' => "父级栏目ID不能少于1个字符",
				'maxlength' => "父级栏目ID不能大于20个字符"
			),
			'partimage' => array(
				'maxlength' => "栏目图片链接大于200个字符"
			),
			'ordernum' => array(
				'isnumber' => '请正确填写栏目排序数值(0-99)',
			),
			'partdescription'=>array(
				'maxlength' => '栏目介绍长度大于500个字符',
			),		
		)
	);

	function check_number($val){
		if (is_numeric($val)) return TRUE;
		else return FALSE;
	}
	//建立树形结构
	function BuildPartTreeLabel($PartTreeRootXMLNode){
		$this->GeneratePartTreeLabel($PartTreeRootXMLNode);
		
		if (is_array($this->m_arrPartTreeLabel)) return $this->m_arrPartTreeLabel;
		else return FALSE;
		
	}
	function GeneratePartTreeLabel($XMLNode,$SignSpace){
	
	$oSubXMLNodelist;
	$sThisSpace;
	$oSubXMLNode;
		
		if ($XMLNode->childNodes->length>0){
			$oSubXMLNodelist=$XMLNode->childNodes;
		}else{
			$oSubXMLNodelist=NULL;
		}
		if (strtoupper($XMLNode->attributes->getNamedItem("NodeId")->nodeValue)==strtoupper($GLOBALS['G_SP']['controller_cms']['root_part_id']))
		{
			
			$this->m_arrPartTreeLabel=array_merge($this->m_arrPartTreeLabel,array(array('fpartid'=>$XMLNode->attributes->getNamedItem("NodeId")->nodeValue,'fpartname'=>$XMLNode->attributes->getNamedItem("Title")->nodeValue)));
			
		}else{
			
			$this->m_arrPartTreeLabel=array_merge($this->m_arrPartTreeLabel,array(array('fpartid'=>$XMLNode->attributes->getNamedItem("NodeId")->nodeValue,'fpartname'=>$SignSpace."|-".$XMLNode->attributes->getNamedItem("Title")->nodeValue)));
			
		}
		
		if (is_null($oSubXMLNodelist)){
		 	return FALSE;
		}
		else{
			$this->m_sSignSpace =$SignSpace."&nbsp;&nbsp;";
			$sThisSpace=$this->m_sSignSpace;
			
			foreach ($oSubXMLNodelist as $oSubXMLNode){
				$this->GeneratePartTreeLabel($oSubXMLNode,$sThisSpace);
			}
			return TRUE;
		}
		
	}
	//添加
	function BuildPartTreeXML(){
		if (is_null($_SESSION['PART_TREE_XML'])){
			$this->m_oXMLDom=new DOMDocument("1.0","utf-8");
			$oXMLRootNode=$this->m_oXMLDom->createElement("Tree");
			
			$oXMLRootNode->setAttribute("TreeId",$GLOBALS['G_SP']['app_id']);
			$oXMLRootNode->setAttribute("Title", $GLOBALS['G_SP']['app_name']);
			$this->m_oXMLDom->appendChild($oXMLRootNode);
			
			$oStartNode=$this->m_oXMLDom->createElement("TreeNode");
			$oStartNode->setAttribute("NodeId",$GLOBALS['G_SP']['controller_cms']['root_part_id']);
			$oStartNode->setAttribute("Title",$GLOBALS['G_SP']['controller_cms']['root_part_name']);
			$oStartNode->setAttribute("Href",spUrl('cms','index'));
			$this->GeneratePartXML($oStartNode);
			$this->m_oXMLDom->documentElement->appendChild($oStartNode);
			$_SESSION['PART_TREE_XML']=$this->m_oXMLDom->saveXML();
			
		}
			return $_SESSION['PART_TREE_XML'];
	}
	
	private function GeneratePartXML(&$XMLNode){
		$condition=array('fpartid'=>$XMLNode->getAttribute("NodeId"));
		$results=spClass("lib_part")->findAll($condition,"partid asc,ordernum desc","partid,partname");
		if (FALSE==$results){
			return FALSE;
		}else{
			foreach ($results as $item){
				$oXMLNode=$this->m_oXMLDom->createElement("TreeNode");
				$oXMLNode->setAttribute("NodeId",$item["partid"]);
				$oXMLNode->setAttribute("Title",$item["partname"]);
				$oXMLNode->setAttribute("Href",spUrl("cms","managerdoc",array('partid'=>$item["partid"])));
				$XMLNode->appendChild($oXMLNode);
			}
			for ($i=0; $i<$XMLNode->childNodes->length; $i++){
				$this->GeneratePartXML($XMLNode->childNodes->item($i));
			}
		}
	}

}